﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.Model;
using YouHP.BLL;

namespace YouHP.Pages.Member
{
    public class TreatmentAction : PatientBasePage
    {
        protected int Type { get; set; }
        protected int THId { get; set; }    /*病人的某条治疗历史纪录id*/
        protected PatientTreatmentInfo PTInfo {get; set;}

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            Type = THId = -1;
        }

        /*判断剂量信息*/
        private bool CheckDosageData()
        {
            try
            {
                PTInfo.DataValue = Convert.ToDouble(RequestTools.GetFormString("dosage"));

                /*还需要判断是否介于剂量上下限之间*/
                int tId = RequestTools.GetFormInt("treatmentid", -1);
                TreatmentInfo tInfo = TreatmentBLL.Instance.GetTreatmentInfo(tId);
                if (tInfo != null)
                {
                    string[] limitValues = tInfo.LimitValues.Split('@');
                    if (limitValues.Length >= 2)
                    {
                        double minValue = Convert.ToDouble(limitValues[0]);
                        double maxValue = Convert.ToDouble(limitValues[1]);
                        if (PTInfo.DataValue < minValue || PTInfo.DataValue > maxValue)
                        {
                            string msg = string.Format("剂量值必须介于{0} 和 {1} 之间", minValue, maxValue);
                            this.AddErrLine(msg);
                            return false;
                        }
                    }
                    PTInfo.Unit = tInfo.Unit; 
                }
            }
            catch
            {
                this.AddErrLine("剂量值必须为整数！");
                return false;
            }

            return true;
        }

        /*检查表单字段值，并装载数据*/
        private bool CheckFormData(int type)
        {
            DateTime curDate = DateTime.Now, tmpDate;
            string msg = "";
            List<TreatmentHistoryInfo> ptHistoryList = null;
            
            PTInfo = new PatientTreatmentInfo();
            PTInfo.TreatmentId = RequestTools.GetFormInt("treatmentid", -1) ;
            PTInfo.PRId = this.PRInfo.Id;

            switch (Type)
            {
                case Constants.PREVIOUS_USED:
                    try
                    {
                        tmpDate = Convert.ToDateTime(RequestTools.GetFormString("startdate"));
                        if (tmpDate > curDate)
                        {
                            this.AddErrLine("开始时间不能晚于当前时间！");
                            return false;
                        }
                        PTInfo.StartDate = tmpDate;
                    }
                    catch
                    {
                        this.AddErrLine("开始时间格式不正确！");
                        return false;
                    }

                    try
                    {
                        tmpDate = Convert.ToDateTime(RequestTools.GetFormString("enddate"));
                        if (tmpDate < PTInfo.StartDate)
                        {
                            this.AddErrLine("结束时间不能比开始时间早！");
                            return false;
                        }
                        else if(tmpDate > curDate)
                        {
                            this.AddErrLine("结束时间不能比当前时间晚！");
                            return false;
                        }
                        PTInfo.EndDate = tmpDate;
                    }
                    catch
                    {
                        this.AddErrLine("结束时间格式不正确！");
                        return false;
                    } 
               
                    /*从历史治疗信息中查询时间是否符合逻辑，并提示用户*/
                    ptHistoryList = PatientTreatmentBLL.Instance.GetAllUseHistoryTreatments(PTInfo.PRId, PTInfo.TreatmentId);
                    foreach (TreatmentHistoryInfo tmpPTInfo in ptHistoryList)
                    {
                        if (tmpPTInfo.State != Constants.CURRENT_USING)         /*表示结束时间已经填写的纪录*/
                        {
                            if (PTInfo.StartDate >= tmpPTInfo.StartDate && PTInfo.StartDate <= tmpPTInfo.EndDate)
                            {
                                msg = string.Format("开始时间不能在已经填写的时间段{0} 和 {1} 之间",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"),
                                    tmpPTInfo.EndDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                            if (PTInfo.EndDate >= tmpPTInfo.StartDate && PTInfo.EndDate <= tmpPTInfo.EndDate)
                            {
                                msg = string.Format("结束时间不能在已经填写的时间段{0} 和 {1} 之间",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"),
                                    tmpPTInfo.EndDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                        }
                        else   
                        {
                            if (PTInfo.StartDate >= tmpPTInfo.StartDate)
                            {
                                msg = string.Format("开始时间不能比您当前使用的开始时间{0}晚",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                            if (PTInfo.EndDate >= tmpPTInfo.StartDate)
                            {
                                msg = string.Format("结束时间不能比您当前使用的开始时间{0}晚",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                        }
                    }

                    if (!CheckDosageData()) /*检查剂量*/
                    {
                        return false;
                    }
                    break;
                case Constants.CURRENT_USING:
                    try
                    {
                        tmpDate = Convert.ToDateTime(RequestTools.GetFormString("startdate"));
                        if (tmpDate > curDate)
                        {
                            this.AddErrLine("开始时间不能晚于当前时间！");
                            return false;
                        }
                        PTInfo.StartDate = tmpDate;
                    }
                    catch
                    {
                        this.AddErrLine("开始时间格式不正确");
                        return false;
                    }

                    /*从历史治疗信息中查询时间是否符合逻辑，并提示用户*/
                /*    ptHistoryList = PatientTreatmentBLL.Instance.GetAllUseHistoryTreatments(PTInfo.PRId, PTInfo.TreatmentId);
                    foreach (PatientTreatmentInfo tmpPTInfo in ptHistoryList)
                    {
                        if (tmpPTInfo.State != Constants.CURRENT_USING) //表示结束时间已经填写的纪录
                        {
                            if (PTInfo.StartDate >= tmpPTInfo.StartDate && PTInfo.StartDate <= tmpPTInfo.EndDate)
                            {
                                msg = string.Format("开始时间不能在已经填写的时间段{0} 和 {1} 之间",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"),
                                    tmpPTInfo.EndDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                        }
                        else
                        {
                            if (PTInfo.StartDate >= tmpPTInfo.StartDate)
                            {
                                msg = string.Format("开始时间不能比您当前使用的开始时间{0}晚",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                        }
                    }
*/
                    if (!CheckDosageData()) /*检查剂量*/
                    {
                        return false;
                    }

                    break;
                case Constants.STOPED_USE:
                    try
                    {
                        tmpDate = Convert.ToDateTime(RequestTools.GetFormString("enddate"));
                        if (tmpDate > curDate)
                        {
                            this.AddErrLine("结束时间不能比开始时间晚！");
                            return false;
                        }
                        PTInfo.EndDate = tmpDate;
                    }
                    catch
                    {
                        this.AddErrLine("结束时间格式不正确！");
                        return false;
                    }

                    /*从历史治疗信息中查询时间是否符合逻辑，并提示用户*/
                    ptHistoryList = PatientTreatmentBLL.Instance.GetAllUseHistoryTreatments(PTInfo.PRId, PTInfo.TreatmentId);
                    foreach (TreatmentHistoryInfo tmpPTInfo in ptHistoryList)
                    {
                        if (tmpPTInfo.State != Constants.CURRENT_USING) /*表示结束时间已经填写的纪录*/
                        {
                            if (PTInfo.EndDate >= tmpPTInfo.StartDate && PTInfo.EndDate <= tmpPTInfo.EndDate)
                            {
                                msg = string.Format("结束时间不能在已经填写的时间段{0} 和 {1} 之间！",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"),
                                    tmpPTInfo.EndDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                        }
                        else
                        {
                            if (PTInfo.EndDate >= tmpPTInfo.StartDate)
                            {
                                msg = string.Format("结束时间不能比您当前使用的开始时间{0}晚！",
                                    tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                        }
                    }
 
                    break;
                case Constants.CHANGE_DOSAGE:
                    try
                    {
                        tmpDate = Convert.ToDateTime(RequestTools.GetFormString("actiondate"));
                        PTInfo.ActionDate = tmpDate;

                        /*剂量改变的时间必须介于该段历史纪录的开始与结束时间之间*/
                        int thId = RequestTools.GetQueryInt("thid", -1);
                        TreatmentHistoryInfo tmpPTInfo = null /*PatientTreatmentBLL.Instance.GetPatientTreatmentById(THId)*/;
                        if (tmpPTInfo != null)
                        {
                            if (tmpPTInfo.State == Constants.CURRENT_USING)
                            {
                                tmpPTInfo.EndDate = DateTime.Now;
                            }
                            if (PTInfo.ActionDate < tmpPTInfo.StartDate || PTInfo.ActionDate > tmpPTInfo.EndDate)
                            {
                                msg = string.Format("改变剂量时间必须介于开始时间与结束时间之间！",
                                     tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                                this.AddErrLine(msg);
                                return false;
                            }
                        }
                    }
                    catch 
                    {
                        this.AddErrLine("时间格式不正确！");
                        return false;
                    }

                    if (!CheckDosageData()) /*检查剂量*/
                    {
                        return false;
                    }
                    break;
                default:
                    break;
            }

            PTInfo.Id = RequestTools.GetFormInt("thid", -1);
            PTInfo.TreatmentId = RequestTools.GetFormInt("treatmentid", -1);
            PTInfo.UsePurpose = RequestTools.GetFormInt("usepurpose", -1);
            PTInfo.StopReason = RequestTools.GetFormInt("stopreason", -1);
            PTInfo.PRId = this.PRInfo.Id;
            PTInfo.UpdateDate = DateTime.Now;

            return true;
        }

        protected void HandlePost()
        {
            Type = RequestTools.GetFormInt("actiontype", -1);
            if (Type < Constants.CURRENT_USING || Type >= Constants.MAX_USEING_SENCE)
            {
                AddErrLine("错误的参数");
                return;
            }

            if (!CheckFormData(Type))
                return;

            switch (Type)
            {
                case Constants.CURRENT_USING:
                    try
                    {
                        PTInfo.EndDate = DateTime.MaxValue;
                        PTInfo.State = Constants.CURRENT_USING;
                        /*if (PatientTreatmentBLL.Instance.AddTreatmentRecorde(PTInfo))   //成功
                        {
                            ResponseHelper.Redirect("TreatmentHistory.aspx?tid=" + PTInfo.TreatmentId);
                        }*/
                    }
                    catch { }
                    break;
                case Constants.PREVIOUS_USED:
                    try
                    {
                        PTInfo.State = Constants.STOPED_USE;
                       /* if (PatientTreatmentBLL.Instance.AddTreatmentRecorde(PTInfo))   //成功
                        {
                            ResponseHelper.Redirect("TreatmentHistory.aspx?tid=" + PTInfo.TreatmentId);
                        }*/
                    }
                    catch { } 
                    break;
                case Constants.STOPED_USE:
                    try
                    {
                        PTInfo.State = Constants.STOPED_USE;
                        //if (PatientTreatmentBLL.Instance.StopTreatmentRecorde(PTInfo))   //成功
                        {
                            ResponseHelper.Redirect("TreatmentHistory.aspx?tid=" + PTInfo.TreatmentId);
                        }
                    }
                    catch { } 
                    break;
                case Constants.CHANGE_DOSAGE:
                    try
                    {
                        //if (PatientTreatmentBLL.Instance.ChangeTreatmentDosage(PTInfo))   //成功
                        {
                            ResponseHelper.Redirect("TreatmentHistory.aspx?tid=" + PTInfo.TreatmentId);
                        }
                    }
                    catch { } 
                    break;
                default:
                    break;            
            }
        }
       
        protected void HandleGet()
        { 
            // tid : Treatment Id
            Type = RequestTools.GetQueryInt("type", -1);

            if (Type == Constants.CURRENT_USING || Type == Constants.PREVIOUS_USED)    /*添加当前使用的治疗方案或者添加完整的历史治疗信息*/
            {
                int tId = RequestTools.GetQueryInt("tid", -1);
                if (tId > 0)
                {
                    TreatmentInfo tInfo = TreatmentBLL.Instance.GetTreatmentInfo(tId);
                    if (tInfo != null)
                    {
                        PTInfo = new PatientTreatmentInfo();
                        PTInfo.TreatmentId = tInfo.Id;
                        PTInfo.TreatmentName = tInfo.Name;
                        PTInfo.Unit = tInfo.Unit;    /*对于只有一种单位的情况直接赋值，在多种选择情况则需要解析字符串，然后提供选项*/
                        //解析上下限字符串
                        string[] limitValues = tInfo.LimitValues.Split('@');
                        if (limitValues.Length >= 2)
                        {
                            PTInfo.MinValue = Convert.ToDouble(limitValues[0]);
                            PTInfo.MaxValue = Convert.ToDouble(limitValues[1]);
                        }

                        return;     //success
                    }
                }
            }
            else if (Type < Constants.MAX_USEING_SENCE) /*停止使用与改变剂量*/
            {
                THId = RequestTools.GetQueryInt("thid", -1);
                if (THId > 0)
                {
                   // PTInfo = PatientTreatmentBLL.Instance.GetPatientTreatmentById(THId);
                    if (PTInfo != null)
                    {
                        PTInfo.EndDate = DateTime.Now ;
                        return; //success
                    }
                }
            }

            this.AddErrLine("无效的参数");
            return;
        }
    }
}
